<?php
/**
 * Created by PhpStorm.
 * User: qiaoli
 * Date: 15/10/15
 * Time: 下午8:38
 */
class Type_weightm extends CI_Model
{
    public function add_post_type_id($post_id,$post_type_id_array,$post_addtime=0)
    {
        if($post_addtime == 0)//如果没有帖子的addtime，取addtime
        {
            $this->load->Model("postm");
            $one_post = $this->postm->one_post_content($post_id,1);
            $post_addtime = $one_post['addtime'];
        }
        foreach($post_type_id_array as $typeid)
        {
            $data = array();
            $data['post_id'] = $post_id;
            $data['type_id'] = $typeid;
            $data['count'] = 1;
            $data['addtime'] = $post_addtime;
            $result = $this->db->insert("ml_posttype",$data);
            if($result)
            {
                $return = $this->db->insert_id();
            }
            else
            {
                $return = false;
            }
        }
    }

    /* 文字问答分类入库 2.2版本 */
    public function add_release_type_id($post_id,$post_type_id_array,$post_addtime=0)
    {
        if($post_addtime == 0)//如果没有帖子的addtime，取addtime
        {
            $post_addtime = time();
            $this->load->Model("postm");
            $one_post = $this->postm->one_post_content($post_id,1);
            if(isset($one_post['addtime']) && $one_post['addtime'])
            {
                $post_addtime = $one_post['addtime'];
            }
        }
        foreach($post_type_id_array as $typeid)
        {
            $data = array();
            $data['post_id'] = $post_id;
            $data['type_id'] = $typeid;
            $data['count'] = 1;
            $data['addtime'] = $post_addtime;
            $result = $this->db->insert("ml_releasetype",$data);
            if($result)
            {
                $return = $this->db->insert_id();
            }
            else
            {
                $return = false;
            }
        }
    }


    public  function update_post_type_postaddtime($post_id,$post_addtime)
    {
        $this->db->set('addtime', $post_addtime);
        $this->db->where("post_id",$post_id);
        return $this->db->update('ml_posttype');
    }

    public function update_post_type_id_count($post_id, $type_id, $count)
    {
        $this->db->set('count', $count);
        $this->db->where(array("post_id" =>$post_id, "type_id" => $type_id));
        return $this->db->update('ml_posttype');
    }
    public function type_id_count($post_id, $type_id)
    {
        $query = $this->db->select("count")->from('ml_posttype')->where('post_id',$post_id)->where('type_id',$type_id)->get()->result_array();
        return $query[0]['count'] ? $query[0]['count'] : 0;
    }
    //获取一个帖子的分类
    public function get_post_type_weight($post_id)
    {
        $query = $this->db->select()->from('ml_posttype')->where('post_id',$post_id)->where('type',1)->where('count>',0)->get()->result_array();
        return $query;
    }

    //更新分类用户记录表
    public function update_post_type_weight($post_id,$type_id,$user_id,$action)
    {
        $sql = 'UPDATE ml_type_weight_log  SET action_count = action_count + 1 where post_id = '.$post_id.' AND type_id = '.$type_id.' AND '.' user_id = '.$user_id;
        $result = $this->db->query($sql);
        if($result)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    //获取用户行为表里面的 记录数
    public function get_type_weight_log_count($post_id,$type_id,$user_id)
    {
        $query = array();
        $query['post_id'] = $post_id;
        $query['type_id'] = $type_id;
        $query['user_id'] = $user_id;
        return $result = $this->db->where($query)->count_all_results("ml_type_weight_log");
    }

    //添加发帖分类权重表
    public function insert_post_type_weight($post_id,$type_id)
    {
        $result = $this->add_post_type_id($post_id,array($type_id));
        return $result;
    }

    //添加发帖分类权重日志表
    public function insert_post_type_weight_log($set)
    {
        $result = $this->db->insert("ml_type_weight_log",$set);
        return $result;
    }

    //文字问答分类权重日志表(2.2版本)
    public function insert_post_type_release_log($set)
    {
        $result = $this->db->insert("ml_type_release_log",$set);
        return $result;
    }

    //获取用户行为表里的 用户行为状态
    public function get_action_count($query)
    {
        $result = $this->db->where($query)->get("ml_type_weight_log")->result_array();
        if($result)
        {
            return $result;
        }
        else
        {
            return array(array("action_count"=>0));
        }
    }

    //获取一个帖子在一个分类下的结果列表
    public function get_post_type_weight_array($type_id,$limit,$offset)
    {
        $result = $this->db->select()->from("ml_posttype")->where('type_id',$type_id)->where('count>',0)->order_by("post_id","desc")->limit($limit,$offset)->get()->result_array();
        return $result;
    }


    /* 获取帖子原有标签 */
    public function get_old_tags_list($post_id, $dis_cate_flag)
    {
        $res = $this->db
            ->select('type_id')
            ->from('ml_posttype')
            ->where(array('post_id'=>$post_id,'type'=> $dis_cate_flag))
            ->get()
            ->result_array();

        if(is_array($res))
        {
            $this->db->where(array('post_id'=>$post_id,'type'=> $dis_cate_flag));
            $this->db->delete('ml_posttype');
        }
        return true;
    }

    /* 添加标签或者分类 */
    public function add_type_insert($type_ids,$addtime,$post_id,$type)
    {
        try
        {
            $data = array();
            $data['addtime'] = $addtime;
            $data['type'] = $type;
            $data['post_id'] = $post_id;
            $data['count'] = 1;
            foreach($type_ids as $v)
            {
                if (!$v) continue;
                $data['type_id'] = $v;
                $this->db->insert('ml_posttype',$data);
            }
            return true;
        }
        catch(Exception $e)
        {
            return false;
        }
    }

}